package com.netease.xone.itemview;

import Utils.StackTraceUtil;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.netease.log.NTLog;


public abstract class AbsLinearLayout extends LinearLayout implements OnClickListener {
    public static final String TAG = AbsLinearLayout.class.getSimpleName();
    
    public static final int DIVIDER_NONE   =  0x00; // 无分割线
    public static final int DIVIDER_HEAD   =  0x02; // 头部分割线
    public static final int DIVIDER_MIDDLE =  0x04; // 中间分割线
    public static final int DIVIDER_FOOT   =  0x08; // 底部分割线
    
    private int mCurrentIndex;
    private OnSelectListener mOnSelectListener;
    
    public AbsLinearLayout(Context context) {
        super(context);
    }

    public AbsLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AbsLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void show () {
        final int count = getCount();
        int orientation = getOrientation();
        View divider = null;
        for (int position = 0; position < count; position++) {
            // Before add
            if (position == 0 && (getDividerStyle()&DIVIDER_HEAD) == DIVIDER_HEAD) {
                divider = getDivider();
                if (orientation == HORIZONTAL) {
                    addView(divider, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                            LayoutParams.MATCH_PARENT));
                } else {
                    addView(divider, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                            LayoutParams.WRAP_CONTENT));
                }
            }
            
            // Adding
            LinearLayout.LayoutParams para = null;
            if (orientation == HORIZONTAL) {
                para = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
            } else {
                para = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            }
            View child = getView(position);
            child.setTag(position);
            child.setOnClickListener(this);
            addView(child, para);
            if (position == mCurrentIndex) {
                child.setSelected(true);
            }
            
            // After added
            if (position != count - 1 && (getDividerStyle()&DIVIDER_MIDDLE) == DIVIDER_MIDDLE) {
                divider = getDivider();
                if (orientation == HORIZONTAL) {
                    addView(divider, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                            LayoutParams.MATCH_PARENT));
                } else {
                    addView(divider, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                            LayoutParams.WRAP_CONTENT));
                }
                
            } else {
                if ((getDividerStyle()&DIVIDER_FOOT) == DIVIDER_FOOT) {
                    divider = getDivider();
                    if (orientation == HORIZONTAL) {
                        addView(divider, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                                LayoutParams.MATCH_PARENT));
                    } else {
                        addView(divider, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                                LayoutParams.WRAP_CONTENT));
                    }
                    
                }
            }
        }
    }

    public void setOnSelectListener(OnSelectListener listener) {
        mOnSelectListener = listener;
    }
    
    @Override
    public void onClick(View v) {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        int index = (Integer)v.getTag();
        if (index == mCurrentIndex) {
            if (mOnSelectListener != null) {
                mOnSelectListener.onReSelected(v, index);
            }
        } else {
            getChildAt(mCurrentIndex).setSelected(false);
            mCurrentIndex = index;
            getChildAt(mCurrentIndex).setSelected(true);
            if (mOnSelectListener != null) {
                mOnSelectListener.onSelected(v, index);
            }
        }
    }
    
    /**
     * 获得当前选中index
     * @return
     */
    public int getCurrentIndex(){
        return mCurrentIndex;
    }
    
    /**
     * 分割线样式
     *
     * @see DIVIDER_NONE
     * @see DIVIDER_START
     * @see DIVIDER_END
     * @see DIVIDER_BOTH
     * @see DIVIDER_MIDDLE
     */
    public abstract int getDividerStyle();

    /**
     * 获取分隔view
     * @return
     */
    public abstract View getDivider();
    
    /**
     * 获取子view数量
     * @return
     */
    public abstract int getCount();

    /**
     * 获取子view
     * @param position
     * @return
     */
    public abstract View getView(int position);
    
    public interface OnSelectListener {
        
        public void onSelected(View v, int index);
        
        public void onReSelected(View v, int index);
    }
}
